import {
  Body,
  Controller,
  Get,
  Post,
  ParseIntPipe,
  Query,
  Put,
  Delete,
  applyDecorators,
  HttpException,
  HttpStatus,
  UseInterceptors,
  ClassSerializerInterceptor,
  SerializeOptions,
} from '@nestjs/common';
import { UserService } from '../../shared/services/user.service';
import { CreateUserDto, UpdateUserDto } from '../../shared/dtos/user.dto';
import { UserEntity } from '../../shared/entities/user.entity';
import {
  ApiBearerAuth,
  ApiBody,
  ApiOperation,
  ApiQuery,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
import { Result } from '../../shared/vo/result';
import { UserVo } from '../../shared/vo/user.vo';

@ApiTags('用户管理')
@SerializeOptions({
  //配置序列化
  // strategy:'excludeAll'   //排除所有的
  strategy: 'exposeAll', //返回所有的
})
@UseInterceptors(ClassSerializerInterceptor)
@Controller('api/user')
export class UserController {
  constructor(private userService: UserService) {}

  // @ApiOperation({ summary: '创建新用户' })
  // // 接口描述，type为返回示例数据，默认状态下是空对象，需要给UserEntity类添加一些装饰器
  // @ApiResponse({ status: 201, description: '创建成功', type: UserEntity })
  // @ApiResponse({ status: 400, description: '请求参数错误' })
  // @ApiBody({ type: CreateUserDto })
  @ApiCreateSwagger()
  @Post('create')
  create(@Body() createUserDto: CreateUserDto) {
    return this.userService.create(createUserDto);
  }

  @ApiFindOneSwagger()
  @Get('get')
  async findOne(@Query('id', ParseIntPipe) id: number) {
    const result = await this.userService.findOne({ where: { id } });
    if (result) {
      const userVo = new UserVo();
      userVo.username = result.username;
      userVo.id = result.id;
      userVo.status = result.status;
      userVo.sort = result.sort;
      userVo.email = result.email;
      userVo.is_super = result.is_super;
      userVo.mobile = result.mobile;
      userVo.createdAt = result.createdAt;
      userVo.updatedAt = result.updatedAt;
      return userVo;
    } else {
      throw new HttpException('用户未找到', HttpStatus.NOT_FOUND);
    }
  }

  @ApiUpdateSwagger()
  @Put('update')
  update(@Body() updateUserDto: UpdateUserDto) {
    return this.userService.update(updateUserDto.id, updateUserDto);
  }

  @ApiDeleteSwagger()
  @Delete('delete')
  delete(@Query('id', ParseIntPipe) id: number) {
    return this.userService.delete(id);
  }

  // @ApiOperation({ summary: '用户列表' })
  // // 接口描述，type为返回示例数据，默认状态下是空对象，需要给CreateUserDto类添加一些装饰器
  // @ApiResponse({ status: 200, description: '查询用户列表', type: [UserEntity] })
  @ApiListSwagger()
  @Get('list')
  list() {
    return this.userService.findAll();
  }
}

function ApiCreateSwagger() {
  return applyDecorators(
    ApiOperation({ summary: '创建新用户' }),
    ApiBearerAuth(),
    ApiResponse({ status: 201, description: '创建成功', type: UserEntity }),
    ApiResponse({ status: 400, description: '请求参数错误' }),
    ApiBody({ type: CreateUserDto }),
  );
}

function ApiFindOneSwagger() {
  return applyDecorators(
    ApiOperation({ summary: 'id查询用户' }),
    ApiQuery({ name: 'id', description: '用户id', type: Number, example: 1 }),
    ApiResponse({ status: 200, description: '查询成功', type: UserVo }),
    ApiResponse({ status: 404, description: '用户未找到' }),
  );
}

function ApiUpdateSwagger() {
  return applyDecorators(
    ApiOperation({ summary: '更新用户' }),
    ApiResponse({ status: 200, description: '更新成功', type: Result }),
    ApiResponse({ status: 400, description: '请求参数错误' }),
    ApiResponse({ status: 404, description: '用户未找到' }),
    ApiBody({ type: UpdateUserDto }),
  );
}

function ApiDeleteSwagger() {
  return applyDecorators(
    ApiOperation({ summary: 'id删除用户' }),
    ApiResponse({ status: 200, description: '删除成功', type: Result }),
    ApiResponse({ status: 400, description: '请求参数错误' }),
    ApiResponse({ status: 404, description: '用户未找到' }),
    ApiQuery({ name: 'id', type: Number, example: 1 }),
  );
}

function ApiListSwagger() {
  return applyDecorators(
    ApiOperation({ summary: '用户列表' }),
    // 接口描述，type为返回示例数据，默认状态下是空对象，需要给CreateUserDto类添加一些装饰器
    ApiResponse({
      status: 200,
      description: '查询用户列表',
      type: [UserEntity],
    }),
  );
}
